package learn_security;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class AESTest {

	public static void main(String[] args) throws NoSuchAlgorithmException, FileNotFoundException, IOException {
		generateKey("secert.key");
	}
	
	private static void encrypt(String msg, String keyFile) throws FileNotFoundException, IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(keyFile))) {
			Key key = (Key)in.readObject();
			Cipher cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
		}
	}

	private static void generateKey(String keyFile) throws NoSuchAlgorithmException, IOException, FileNotFoundException {
		KeyGenerator keygen = KeyGenerator.getInstance("AES");
		SecureRandom random = new SecureRandom();
		keygen.init(random);
		SecretKey key = keygen.generateKey();
		try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(keyFile))) {
			out.writeObject(key);
		}
	}

}
